﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamAnalysis.Model;
using Ext.Net;

namespace ExamAnalysis.Input
{
    public partial class InputStudentInExam : System.Web.UI.Page
    {
        static int classid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newStudent = from s in db.StudentInfo
                                     select s;
                    this.Store2.DataSource = newStudent;
                    this.Store2.DataBind();
                    var newClass = from c in db.ClassInfo
                                   join s in db.SchoolInfo on c.SchoolID equals s.SchoolID
                                   select new { SchoolID = c.SchoolID, ClassID = c.ClassID, FullName = s.FullName, ClassCode = c.ClassCode, ClassName = c.ClassName };
                    this.Store1.DataSource = newClass;
                    this.Store1.DataBind();
                    Store_ExamInfo.DataSource = new ExamAnalysisDataContext().ExamInfo;
                    Store_ExamInfo.DataBind();
                }
            }
        }

        protected void RowSelect_ExamInfo(object sender, DirectEventArgs e)
        {
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            int ExamID = 0;
            try
            {
                ExamID = int.Parse(e.ExtraParams["ExamID"]);
            }
            catch (FormatException ex)
            {
                return;
            }
            if (ExamID != -1)
            {
                var list = from s in db.StudentInExam
                           where s.ExamID == ExamID
                           select new { s.StudentID, s.ExamID, s.StudentInfo.StudentName };
                Store_StInExem.DataSource = list;
                Store_StInExem.DataBind();
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                classid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myClass = from c in db.ClassInfo
                              where c.ClassID == classid
                              select c.ClassName;
                foreach (var Class in myClass)
                {
                    DropDownField1.Text = Class.ToString();
                }
                var newStudent = from s in db.StudentInfo
                                 where s.ClassID == classid
                                 select s;
                this.Store2.DataSource = newStudent;
                this.Store2.DataBind();
            }
            classid = 0;
        }

        protected void Button5_DirectClick(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel_ExamInfo.GetSelectionModel() as RowSelectionModel;
            int examId = 0;
            int stID = 0;
            if (sm.SelectedRows.Count != 0)
                examId = int.Parse(sm.SelectedRows[0].RecordID);
            else return;

            RowSelectionModel sm2 = this.GridPanel_StInExem.GetSelectionModel() as RowSelectionModel;
            if (sm2.SelectedRows.Count != 0)
                stID = int.Parse(sm2.SelectedRows[0].RecordID);
            else return;
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            db.StudentInExam.DeleteAllOnSubmit(from eg in db.StudentInExam
                                               where eg.ExamID == examId && eg.StudentID == stID
                                               select eg);
            db.SubmitChanges();
            var list = from s in db.StudentInExam
                       where s.ExamID == examId
                       select new { s.StudentID, s.ExamID, s.StudentInfo.StudentName };
            Store_StInExem.DataSource = list;
            Store_StInExem.DataBind();
        }

        protected void Button6_DirectClick(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel_ExamInfo.GetSelectionModel() as RowSelectionModel;
            int examId = 0;
            int stID = 0;
            if (sm.SelectedRows.Count != 0)
                examId = int.Parse(sm.SelectedRows[0].RecordID);
            else return;

            RowSelectionModel sm2 = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm2.SelectedRows.Count != 0)
                stID = int.Parse(sm2.SelectedRows[0].RecordID);
            else return;
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            db.StudentInExam.DeleteAllOnSubmit(from eg in db.StudentInExam
                                               where eg.ExamID == examId
                                               select eg);
            db.SubmitChanges();
            var list = from s in db.StudentInExam
                       where s.ExamID == examId
                       select new { s.StudentID, s.ExamID, s.StudentInfo.StudentName };
            Store_StInExem.DataSource = list;
            Store_StInExem.DataBind();
        }

        protected void Button7_DirectClick(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel_ExamInfo.GetSelectionModel() as RowSelectionModel;
            int examId = 0;
            int stID = 0;
            if (sm.SelectedRows.Count != 0)
                examId = int.Parse(sm.SelectedRows[0].RecordID);
            else return;

            RowSelectionModel sm2 = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm2.SelectedRows.Count != 0)
                stID = int.Parse(sm2.SelectedRows[0].RecordID);
            else return;
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            db.StudentInExam.InsertOnSubmit(new StudentInExam() { StudentID = stID, ExamID = examId, Score = 0 });
            try { db.SubmitChanges(); }
            catch { }

            var list = from s in db.StudentInExam
                       where s.ExamID == examId
                       select new { s.StudentID, s.ExamID, s.StudentInfo.StudentName };
            Store_StInExem.DataSource = list;
            Store_StInExem.DataBind();
        }

        protected void Button8_DirectClick(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel_ExamInfo.GetSelectionModel() as RowSelectionModel;
            int examId = 0;
            int stID = 0;
            int classid = 0;
            if (sm.SelectedRows.Count != 0)
                examId = int.Parse(sm.SelectedRows[0].RecordID);
            else return;

            RowSelectionModel sm3 = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            if (sm3.SelectedRows.Count != 0)
                classid = int.Parse(sm3.SelectedRows[0].RecordID);


            RowSelectionModel sm2 = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm2.SelectedRows.Count != 0)
                stID = int.Parse(sm2.SelectedRows[0].RecordID);
            else return;
            ExamAnalysisDataContext db = new ExamAnalysisDataContext();
            List<int> ls = null;
            if (classid != 0)
            {
                 ls = (from s in db.StudentInfo
        where s.ClassID == classid
        select s.StudentID).ToList();
            }
            else
                ls = (from s in db.StudentInfo
                      select s.StudentID).ToList();

            db.StudentInExam.DeleteAllOnSubmit(db.StudentInExam.Where(t => t.StudentInfo.ClassID == classid));
            if(classid==0)
                db.StudentInExam.DeleteAllOnSubmit(db.StudentInExam)
;
            foreach (int s in ls)
            {
                db.StudentInExam.InsertOnSubmit(new StudentInExam() { StudentID = s, ExamID = examId, Score = 0 });

            }
           // db.StudentInExam.InsertOnSubmit(new StudentInExam() { StudentID = stID, ExamID = examId, Score = 0 });
            db.SubmitChanges();
            var list = from s in db.StudentInExam
                       where s.ExamID == examId
                       select new { s.StudentID, s.ExamID, s.StudentInfo.StudentName };
            Store_StInExem.DataSource = list;
            Store_StInExem.DataBind();
        }

    }
}